
var id = getQuery("id");
window.onload = function (){
    initProduct();
}

/**
 * 初始化商品信息
 */
function initProduct(){
    axios.get("/product/findById",{
        params:{id}
    }).then(resp =>{
        var info = resp.data;

        $("productImg").src = "/productImg/"+info.imgPath;
        $("nameSpan").innerHTML = info.name;
        $("priceSpan").innerHTML = info.price;
        $("infoSpan").innerHTML = info.infoNum;

        var str = "";
        info.infoList.forEach(n =>{
           str += `<tr><td>${n.userObj.nickName}</td><td>${n.content}</td></tr>`;
        });
        $("data").innerHTML = str;
    })
}

/**
 * 添加评论
 */
function addInfo(){
    axios.get("/info/add",{
        params: {
            productId:id,
            content:$("infoTxt").value
        }
    }).then(resp =>{
        var info = resp.data;
        console.log("resp.data="+info);
        if(info == "ok"){
            $("infoTxt").value = "";
            initProduct();
        }
        else {
            location.href = "/login.html?errorInfo=noGrade";
        }
    })
}